require "common"

--排列3 组选和值
local Pl3BetNumPoint = { 1,2,2,4,5,6,8,10,11,13,14,14,15,15,14,14,13,11,10,8,6,5,4,2,2,1 };

local Pl3BetNumPointz3 = {  1, 2, 1, 3, 3, 3, 4, 5, 4, 5, 5, 4, 5, 5, 4, 5,
						5, 4, 5, 4, 3, 3, 3, 1, 2, 1, 0 };   --组三和值

local Pl3BetNumPointz6 = { 0, 0, 1, 1, 2, 3, 4, 5, 7, 8, 9, 10, 10, 10, 10,
9, 8, 7, 5, 4, 3, 2, 1, 1, 0, 0, 0 };     ---组六和值

local Pl3BetNumPointzx = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75,
						75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1 }  --直选和值
--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len,1 do
                c = string.sub(num,i,i)
                if tonumber(c) > max or tonumber(c) < 0 then return nil end

        end
        return true
end
--数字字符串排序

function sort_str_pl3( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, string.sub(num,i,i) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end

	return out

end

--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_pl3(num)
	for i=1,len do
		c = string.sub(str,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end

--============================判断字符串是否有相交=============================
function is_str_mix_pl3( str1, str2 )
	local t1 = ""
	local len1 = string.len(str1)
	local len2 = string.len(str2)

	for i=1,len1 do
		t1 = string.sub(str1,i,i)
		for j=1,len2 do
			if( t1 == string.sub(str2,j,j) ) then return 1 end
		end
	end

	return nil
end

--返回注数,倍投数 , 投注串, 指令串
function random_pl3( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  8#3
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(3,0,1,2,3,4,5,6,7,8,9)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_pl3('1', zlStr)
	end
	return butNum, butPower, butStr, zlStr
end

--返回注数,倍投数,投注串
function check_pl3( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 51#注码 51#12345#45623 表示自选2注
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 3  then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
	--直选复式 86#第1位#第2位#第3位
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 3  then return 0 end
		for i=1,#xItem  do
			_,distNum = get_distinct_num(xItem[i])
			if distNum ~= string.len(xItem[i]) then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
		butNum = string.len(xItem[1])*string.len(xItem[2])*string.len(xItem[3])
	--组三复式 87#注码  87#2345
	elseif butType == '3' then
		xItem = Split(itemList[1], "#")
		str_len = string.len(xItem[1])
		if #xItem ~= 1  then return 0 end
		_,distNum = get_distinct_num(xItem[1])
			if distNum ~= string.len(xItem[1]) then return 0 end
		if  not judge_str(xItem[1],9) then return 0 end
		if str_len < 2 or str_len > 10 then return 0 end
		butNum = permutation(str_len,2)
	--组六复式 88#注码  87#2345
	elseif butType == '4' then
		xItem = Split(itemList[1], "#")
		str_len = string.len(xItem[1])
		if #xItem ~= 1  then return 0 end
		_,distNum = get_distinct_num(xItem[1])
		if distNum ~= string.len(xItem[1]) then return 0 end
		if  not judge_str(xItem[1],9) then return 0 end
		if str_len < 3 or str_len > 10 then return 0 end
		butNum = combination(str_len,3)
	--组选和值  811#注码
	elseif butType == '5' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1  then return 0 end
		str_len = string.len(xItem[1])
		if str_len > 2 or str_len < 1 then return 0 end
		if  tonumber(xItem[1]) > 26 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = Pl3BetNumPoint[tonumber(xItem[1])]
	--组三和值  812#注码
	elseif butType == '6' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1  then return 0 end
		if tonumber(itemList[1]) > 26 or  tonumber(itemList[1]) < 1 then return 0 end
		butNum = Pl3BetNumPointz3[tonumber(itemList[1])]
	--组六和值   813#注码
	elseif butType == '7' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1  then return 0 end
		if tonumber(itemList[1]) > 24 or  tonumber(itemList[1]) < 3 then return 0 end
		butNum = Pl3BetNumPointz6[tonumber(itemList[1])]
		--胆拖 组六 817#注码到10658519
	elseif butType == '8' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 2 then return 0 end
		if string.len(xItem[1]) > 2 or (string.len(xItem[2]) + string.len(xItem[1])) < 4  then return 0 end
		if not judge_str(xItem[1],9) or not judge_str(xItem[2],9)   then return 0 end
		if  is_str_mix_pl3(xItem[1],xItem[2])  then return 0 end
		butNum = combination(string.len(xItem[2]),(3-string.len(xItem[1])))
	--组选单式  85#注码
	elseif butType == '9' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then return 0 end
		for i=1,#xItem do
			_, distNum =get_distinct_num(xItem[i])
			if distNum == 1 then return 0 end
			if string.len(xItem[i]) ~= 3  then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
	--直选和值
	elseif butType == 'a' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1  then return 0 end
		if tonumber(itemList[1]) > 27 or  tonumber(itemList[1]) < 0 then return 0 end
		butNum = Pl3BetNumPointzx[tonumber(itemList[1]) + 1]

	--[[elseif butType == 'b' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 3 then return 0 end
		if not judge_str(xItem[1],9) or not judge_str(xItem[2],9) or not judge_str(xItem[2],9)  then return 0 end
		butNum = string.len(xItem[1]) * string.len(xItem[1]) * string.len(xItem[1])
	]]--
	else
		return 0
	end
	return butNum,butPower,inStr,inStr
end

--数字字符串排序
function sort_str_dj( num )
	local out = ""
	local st = {}
	local len = 0
	len = string.len(num)
	for i=1,len do
		c = string.sub(num,i,i)
		table.insert( st, c )
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end

function query_pl3(butType,queryStr,djStr)
	local butPower = 1;
	local itemList = {}
	local result = {0,0,0}
	local distnum = ""
	local hezhi = 0
	local str = {}
	local bullStr = {}
	local distdj = ""
	local bingoNum = 0
	itemList = Split( queryStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	jItem = Split(itemList[1],"#")
	--单式自选
	if butType == '1' then
		result[1] = MultSimpCompute(jItem,djStr,butPower,3,1,butType)
	--复式自选
	elseif butType == '2' then
		result[1] = MultSimpCompute(jItem,djStr,butPower,3,1,butType)
	--组三复式
	elseif butType == '3' then
		distnum = get_distinct_num(djStr)

		if string.len(distnum) ~= 2 then
			result[2] = 0
		else
			result[2] = MultSimpCompute(jItem,distnum,butPower,2,1,butType)
		end
	--组六复式
	elseif butType == '4' then
		distnum = get_distinct_num(djStr)

		if string.len(distnum) ~= 3 then
			result[3] = 0
		else
			butType = '3'
			result[3] = MultSimpCompute(jItem,distnum,butPower,3,1,butType)
		end
	--组三和值
	elseif butType == '6' then
		distnum = get_distinct_num(djStr)

		if string.len(distnum) ~= 2 then
			result[2] = 0
		else
			hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
			if tonumber(jItem[1]) == hezhi then
				result[2] = 1*butPower
			end
		end
	--组六和值
	elseif butType == '7' then
		distnum = get_distinct_num(djStr)

		if string.len(distnum) ~= 3 then
			result[3] = 0
		else
			hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
			if tonumber(jItem[1]) == hezhi then
				result[3] = 1*butPower
			end
		end
	--组六胆拖
	elseif butType == '8' then
		distnum = get_distinct_num(djStr)

		if string.len(distnum) ~= 3 then
			result[3] = 0
		else
			butType = '4'
			result[3] = MultSimpCompute(jItem,distnum,butPower,3,1,butType)
		end
	--组选单式
	elseif butType == '9' then
		for i = 1,#jItem do
			distnum = get_distinct_num(jItem[i])
			str[1] = distnum
			distdj = get_distinct_num(djStr)
			if string.len(distnum) == 2 and string.len(distdj) == 2 then
				djStr = sort_str_dj(djStr);
				jItem[i] = sort_str_dj(jItem[i]);
				if djStr == jItem[i] then
					result[2] = result[2] + butPower
				end
			elseif string.len(distnum) == 3 and string.len(distdj) == 3 then
				butType = '3'
				result[3] = result[3] + MultSimpCompute(str,djStr,butPower,3,1,butType)
			end
		end
	--直选和值
	elseif butType == 'a' then
		hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
		if tonumber(jItem[1]) == hezhi then
			result[1] = butPower
		end
--[[	elseif butType == 'b' then
	for i = 1, string.len(djStr) do
		bullStr[i] = string.sub(djStr,i)
	end
	for i = 1,#jItem do
		if string.find(jItem[i], bullStr[i]) ~= nil then
			bingoNum = bingoNum + 1
		end
	end
	if bingoNum == 3 then
		result[1] = butPower

	end]]--
	else
		return 0

	end

	return result[1]..","..result[2]..","..result[3]

end

--print(random_pl3('1',"5"))
--print(check_pl3('1',"123#B10"))
--print(check_pl3('b',"123#123#123#B10"))
--print(check_pl3('2',"0123456789#0123456789#0123456789#B49"))
--print(check_pl3('3',"121#B10"))
--print(check_pl3('4',"13242#B10"))
--print(check_pl3('5',"26#B10"))
--print(check_pl3('6',"9"))
--print(check_pl3('7',"2"))
--print(check_pl3('8',"3#246"))
--print(check_pl3('9',"333#122#123#B12"))
--print(check_pl3('a',"16#B12"))
--print(query_pl3('1',"132#123#B3","123"))
--print(query_pl3('2',"132#123#123#B34","123"))
--print(query_pl3('3',"12#B34","121"))
--print(query_pl3('4',"012369#B1","688"))
--print(query_pl3('6',"9#B5","441"))
--print(query_pl3('7',"10#B5","325"))
--print(query_pl3('8',"6#234567","325"))
--print(query_pl3('9',"123#212#128#111","123"))
--print(query_pl3('a',"10","127"))
--print(query_pl3('b',"123#123#124","123"))
